package com.lei.app;

import com.lei.app.entity.socket.room.SocketRoom;
import com.lei.app.entity.socket.room.SocketRoomUser;
import org.nutz.boot.NbApp;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.ChainBy;
import org.nutz.mvc.annotation.Encoding;
import org.nutz.mvc.annotation.Localization;
import org.nutz.mvc.annotation.Modules;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊泉
 */
@IocBean(create="init", depose="depose")
@Modules(packages = "com.lei.app")
@Localization(value = "locales/", defaultLocalizationKey = "zh_CN")
@Encoding(input = "UTF-8", output = "UTF-8")
@ChainBy(args = "chain/nutzwk-mvc-chain.json")
public class MainLauncher {
    
    @Inject
    protected PropertiesProxy conf;
    @Inject
    protected Dao dao;
    
    public void init() {
        Daos.createTablesInPackage(dao, "com.lei.app.entity" , false );
        //统一据库表结构
        Daos.migration(dao, "com.lei.app.entity", true, false, false);
        initDb();
    }
    public void depose() {}

    public static void main(String[] args) throws Exception {
        new NbApp().setArgs(args).setPrintProcDoc(true).run();
    }


    private void initDb(){
        if(0==dao.count(SocketRoom.class,Cnd.where("delFlag","=",0))){
            SocketRoom room = new SocketRoom();
            room.setMaxNumber(20);
            room.setName("公共房间");
            room.setNote("公共房间");
            room.setCtIp("127.0.0.1");
            room.setSysState(true);
            room = dao.insert(room);
            List<SocketRoomUser> users = new ArrayList<>(room.getMaxNumber());
            for (int i = 0; i < room.getMaxNumber(); i++) {
                SocketRoomUser u = new SocketRoomUser();
                u.setCtIp("127.0.0.1");
                u.setName("测试账号"+(i+1));
                u.setNote("测试账号1"+(i+1));
                u.setSysState(true);
                users.add(u);
            }
            users = dao.insert(users);

            List<NutMap> m = new ArrayList<>(users.size());
            for (SocketRoomUser u : users) {
                m.add(NutMap.NEW().setv("roomId",room.getUuid()).setv("userId",u.getUuid()));
            }
            m.get(0).addv(".table","web_socket_room_user");
            dao.insert(m);
        }
    }

}
